//src/main/java/com/cuit/blog/controller/PictrueController.java
package com.cuit.blog.controller;
import com.cuit.blog.entity.Pictrue;
    import com.cuit.blog.service.PictureService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.*;

    import java.util.List;import org.springframework.ui.Model;

    @Controller
    @RequestMapping("/pictrue")
    public class PictrueController {

        @Autowired
        private PictureService pictureService;

        @GetMapping("/findAll")
        public String findAll(Model model) {
            List<Pictrue> pictures = pictureService.findAll();
            model.addAttribute("pictures", pictures);
            return "pictrue/pictrue";
        }
        @GetMapping("/add")
        public String showAddPage(Model model) {

            Pictrue pictrue=new Pictrue();
            model.addAttribute("pictrue", pictrue);
            return "pictrue/add";
        }
        @PostMapping("/save")
        public String savePicture(@ModelAttribute Pictrue picture) {
            pictureService.save(picture);
            return "redirect:/pictrue/findAll";
        }
        @GetMapping("/delete/{id}")
        public String showDeletePage(@PathVariable Integer id, Model model) {
            Pictrue picture = pictureService.findById(id);
            model.addAttribute("picture", picture);
            return "pictrue/delete";
        }

        @PostMapping("/delete/{id}")
        public String deletePicture(@PathVariable Integer id) {
            pictureService.deleteById(id);
            return "redirect:/pictrue/findAll";
        }
        @GetMapping("/edit/{id}")
        public String showEditPage(@PathVariable Integer id, Model model) {
            Pictrue picture = pictureService.findById(id);
            model.addAttribute("picture", picture);
            return "pictrue/edit";
        }

        @PostMapping("/edit/{id}")
        public String editPicture(@PathVariable Integer id, @ModelAttribute Pictrue picture) {

            picture.setId(id);
            pictureService.save(picture);
            return "redirect:/pictrue/findAll";
        }
        @GetMapping("/search")
        public String showSearchPage() {
            return "pictrue/search";
        }

        @PostMapping("/searchResult")
        public String searchResult(@RequestParam Integer pictureId, ModelMap model) {
            Pictrue picture = pictureService.findById(pictureId);
            model.addAttribute("picture", picture);
            return "pictrue/result";
        }
    }
